package com.home.free.schedule;

import com.home.free.entity.Memory;
import com.home.free.manager.MemoryManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * 统计内存的情况信息
 * @author gaotianjie
 */
//@Configuration
//@EnableScheduling
public class MemorySchedule implements ISchedule {

    @Autowired
    private MemoryManager memoryManager;

    @Override
    @Scheduled(cron = "*/2 * * * * ? ")
    public void run() {
        Memory memory = new Memory();
        memory.setCreateTime((int) (System.currentTimeMillis() / 1000));
        memory.setRunTime((int) (System.currentTimeMillis() / 1000));
        memory.setFreeMemory(Runtime.getRuntime().freeMemory());
        memory.setMaxMemory(Runtime.getRuntime().maxMemory());
        memory.setTotalMemory(Runtime.getRuntime().totalMemory());
        memory.setUsableMemory(Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory() + Runtime.getRuntime().freeMemory());
        memoryManager.insert(memory);
    }
}
